<?php
/**
 * @ClassName: Membercation
 * @Description: 实名认证
 * @author: gydtrade
 * @Date: 2024-09-09 22:42
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\enum\CommonEnum;
use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Membercation extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\MemberCation();
        $this->pageTitle = '实名认证';
        $this->enableField = 'auth_status';
        $this->pagesize = 10;

        $this->indexWith = ['member'];
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->select('member_id', '用户')->dataUrl(url('/admin/member/selectPage'));
        $search->text('realname', '姓名');
        $search->text('idcard', '身份证号');
        $search->select('auth_status', '认证状态')->options(CommonEnum::examineText(true));
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['member_id'])) {
            $where[] = ['member_id', 'eq', $searchData['member_id']];
        }
        if (!empty($searchData['realname'])) {
            $where[] = ['realname', 'like', '%' . $searchData['realname'] . '%'];
        }
        if (!empty($searchData['idcard'])) {
            $where[] = ['idcard', 'like', '%' . $searchData['idcard'] . '%'];
        }
        if (!empty($searchData['auth_status'])) {
            $where[] = ['auth_status', 'eq', $searchData['auth_status']];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->match('member_id', '用户')->to('{member_id}#{member.nickname}');
        $table->show('realname', '姓名');
        $table->show('idcard', '身份证号');
        $table->image('card_front', '身份证正面');
        $table->image('card_back', '身份证反面');
        $table->match('auth_status', '认证状态')->options(CommonEnum::examineText(true))
            ->mapClassGroup([[0, 'info'], [1, 'success'], [2, 'danger']]);

        $table->fields('create_times', '申请/修改时间')->with(
            $table->show('create_time', '申请时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnEnableAndDisable('通过', '不通过')
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $form->select('member_id', '用户')->dataUrl(url('/admin/member/selectPage'))->required();
        $form->text('realname', '姓名')->required();
        $form->radio('sex', '性别')->options([1 => '男', 2 => '女'])->required();
        $form->text('nation', '名族')->required();
        $form->text('birthday', '生日')->required();
        $form->text('address', '地址')->required();
        $form->image('card_front', '身份证正面')->required();
        $form->image('card_back', '身份证反面')->required();
        $form->radio('auth_status', '认证状态')->options(CommonEnum::examineText(true))->required();

        if ($isEdit) {
            $form->show('create_time', '申请时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 审核
     * @param $state
     * @return \think\response\Json
     */
    public function enable($state)
    {
        $ids = input('post.ids', '');
        $ids = array_filter(explode(',', $ids), 'strlen');

        if (empty($ids)) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }

        if ($state) {
            $remark = '平台审核通过';
            $status = 1;
        } else {
            $remark = '平台审核不通过';
            $status = 2;
        }
        $res = 0;
        foreach ($ids as $id) {
            if ($this->trans($id, $remark, $status)) {
                $res += 1;
            }
        }
        if ($res) {
            return json(['code' => 1, 'msg' => '成功操作' . $res . '条数据']);
        } else {
            trace('审核操作失败');
            return json(['code' => 0, 'msg' => '审核操作失败']);
        }
    }

    /**
     * 审核操作
     * @param $id
     * @param $remark
     * @param $state
     * @return model\MemberCation|\think\response\Json
     */
    private function trans($id, $remark, $state = 1)
    {
        $records = model\MemberCation::where('id', $id)->find();

        if (!$records) {
            return json(['code' => 0, 'msg' => '认证信息不存在']);
        }
        if ($records['status'] != 0) {
            return json(['code' => 0, 'msg' => '认证信息不是待审核状态']);
        }

        $up_data = [
            'auth_status' => $state,
            'audit_opinion' => $remark,
            'update_time' => date_now(),
        ];
        if ($state == 1) {
            $up_data['audit_time'] = date_now();
        }
        $res = true;
        $res = model\MemberCation::where('id', $id)->update($up_data);

        return $res;
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'member_id',
            'realname',
            'sex',
            'nation',
            'birthday',
            'address',
            'card_front',
            'card_back',
            'auth_status'
        ], 'post');

        $result = $this->validate($data, [
            'member_id|用户' => 'require',
            'realname|姓名' => 'require',
            'sex|性别' => 'require',
            'nation|名族' => 'require',
            'birthday|生日' => 'require',
            'address|地址' => 'require',
            'card_front|身份证正面' => 'require',
            'card_back|身份证反面' => 'require',
            'auth_status|认证状态' => 'require',
        ]);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}